package com.usabb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.usabb.api.dto.RoundDTO;
import com.usabb.api.dto.RoundTypeDTO;
import com.usabb.enums.BizTypeEnum;
import com.usabb.pojo.vo.AmenityAddVO;
import org.springframework.stereotype.Service;

import com.usabb.dao.RoundRelationDao;
import com.usabb.domain.RoundRelationDO;
import com.usabb.service.RoundRelationService;
import com.ifast.common.base.CoreServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <pre>
 * 周边关联表
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Service
public class RoundRelationServiceImpl extends CoreServiceImpl<RoundRelationDao, RoundRelationDO> implements RoundRelationService {

    @Override
    public List<RoundRelationDO> getListBy(final String bizType, final Long bizId) {
        Wrapper<RoundRelationDO> wrapper = generateDOWrapper(bizType, bizId);

        return this.selectList(wrapper);
    }

    /**
     * TODO: need to refactor
     */
    @Override
    public List<RoundTypeDTO> getRoundTypeDTOListBy(BizTypeEnum bizTypeEnum, Long bizId) {
        List<RoundTypeDTO> roundTypeDTOS = new ArrayList<>();
        List<RoundRelationDO> roundRelationDOS = getListBy(bizTypeEnum.name(), bizId);
        for (RoundRelationDO roundRelationDO : roundRelationDOS) {
            int number = -1;
            if (!CollectionUtils.isEmpty(roundTypeDTOS)) {
                for (int i = 0; i < roundTypeDTOS.size(); i++) {
                    if (roundTypeDTOS.get(i).getName().equals(roundRelationDO.getRoundTypeName())) {
                        number = i;
                        break;
                    }
                }
            }
            if (number == -1) {
                RoundTypeDTO roundTypeDTO = new RoundTypeDTO();
                roundTypeDTO.setName(roundRelationDO.getRoundTypeName());

                RoundDTO roundDTO = new RoundDTO();
                roundDTO.setAutoDuration(roundRelationDO.getAutoDuration());
                roundDTO.setName(roundRelationDO.getName());

                List<RoundDTO> roundDTOS = new ArrayList<>();
                roundDTOS.add(roundDTO);
                roundTypeDTOS.add(roundTypeDTO);

                roundTypeDTO.setRounds(roundDTOS);
            } else {
                RoundDTO roundDTO = new RoundDTO();
                roundDTO.setAutoDuration(roundRelationDO.getAutoDuration());
                roundDTO.setName(roundRelationDO.getName());

                roundTypeDTOS.get(number).getRounds().add(roundDTO);
            }
        }

        return roundTypeDTOS;
    }

    @Override
    public boolean saveOrUpdate(AmenityAddVO addVO) {
        return saveOrUpdate(addVO.getBizType(), addVO.getBaseId(),
                addVO.getRoundTypeId(), addVO.getRoundName(), addVO.getAutoDuration());
    }

    @Override
    public boolean saveOrUpdate(String bizType, Long bizId, Long[] roundTypeIds, String[] roundNames, String[] autoDurations) {
        deleteBy(bizType, bizId);

        return insert(bizType, bizId, roundTypeIds, roundNames, autoDurations);
    }

    public boolean insert(String bizType, Long bizId, Long[] roundTypeIds, String[] roundNames, String[] autoDurations) {
        List<RoundRelationDO> list = new ArrayList<>(roundTypeIds.length);
        for (int i=0; i<roundTypeIds.length; i++) {
            RoundRelationDO relationDO = new RoundRelationDO();
            relationDO.setBizType(bizType);
            relationDO.setBizId(bizId);
            relationDO.setRoundId(roundTypeIds[i]);
            switch (roundTypeIds[i].intValue()){
                case 1:relationDO.setRoundTypeName("商场");
                    break;
                case 2:relationDO.setRoundTypeName("医院");
                    break;
                case 3:relationDO.setRoundTypeName("餐厅");
                    break;
                case 4:relationDO.setRoundTypeName("景点");
                    break;
            }
            relationDO.setName(roundNames[i]);
            relationDO.setAutoDuration(autoDurations[i]);

            list.add(relationDO);
        }

        return this.insertBatch(list);
    }

    @Override
    public boolean deleteBy(String bizType, Long bizId) {
        Wrapper<RoundRelationDO> wrapper = generateDOWrapper(bizType, bizId);

        boolean delete = this.delete(wrapper);
        log.info("Deleted RoundRelationDOs. bizId:{}, result:{}", bizId, delete);
        return delete;
    }

    private static Wrapper<RoundRelationDO> generateDOWrapper(Long bizId) {
        RoundRelationDO param = new RoundRelationDO();
        param.setBizId(bizId);
        return new EntityWrapper<>(param);
    }

    private static Wrapper<RoundRelationDO> generateDOWrapper(String bizType, Long bizId) {
        RoundRelationDO param = new RoundRelationDO();
        param.setBizId(bizId);
        param.setBizType(bizType);
        return new EntityWrapper<>(param);
    }


}
